﻿/*
    Copyright 2008 Travis Scholtens
    
    This file is part of Anticipa-Do List.

    Anticipa-Do List is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Anticipa-Do List is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Anticipa-Do List.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Anticipa_Do_List
{
    /// <summary>
    /// Interaction logic for NotifierListWindow.xaml
    /// </summary>
    partial class NotifierListWindow : Window
    {
        public NotifierListWindow(List<Notifier> notifiers,
            Dictionary<string, Dictionary<string, string>> config)
        {
            InitializeComponent();

            Notifiers = notifiers;
            Configs = config;
        }

        private List<Notifier> Notifiers;
        private Dictionary<string, Dictionary<string, string>> Configs;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            double h = buttonRow.ActualHeight;
            int i;
            for (i = 0; i < Notifiers.Count; i++)
            {
                var label = new Label();
                label.Content = Notifiers[i].NotificationManager.Description;
                label.Margin = new Thickness(10);

                var button = new Button();
                button.Content = "Configure...";
                button.Tag = i;
                button.Click += new RoutedEventHandler(config_Click);
                button.Margin = new Thickness(10);

                var enabled = new CheckBox();
                enabled.IsChecked = Notifiers[i].Enabled;
                enabled.Content = "Enabled";
                enabled.Tag = Notifiers[i];
                enabled.Checked += new RoutedEventHandler(enabled_CheckedChanged);
                enabled.Margin = new Thickness(10);

                var grid = new Grid();
                grid.ColumnDefinitions.Add(new ColumnDefinition());
                grid.ColumnDefinitions.Add(new ColumnDefinition());
                grid.ColumnDefinitions.Add(new ColumnDefinition());
                grid.Children.Add(label);
                grid.Children.Add(button);
                grid.Children.Add(enabled);
                button.SetValue(Grid.ColumnProperty, 1);
                enabled.SetValue(Grid.ColumnProperty, 2);

                panel.Children.Add(grid);

                Height += h;
                contentRow.Height = new GridLength(contentRow.ActualHeight + h);
            }
        }

        private void config_Click(object sender, RoutedEventArgs e)
        {
            int i = (int)((FrameworkElement)sender).Tag;

            var type = Notifiers[i].Type;
            var desc = Notifiers[i].NotificationManager.Description;
            var keys = Notifiers[i].NotificationManager.GetConfigurationKeys();

            var w = Configs.ContainsKey(type) ?
                new ConfigureNotifierWindow(desc, keys, Configs[type]) :
                new ConfigureNotifierWindow(desc, keys);
            bool? res = w.ShowDialog();
            if (res != null && res.Value)
            {
                if (!Configs.ContainsKey(type))
                {
                    var variables = new Dictionary<string, string>();
                    for (int j = 0; j < keys.Count; j++)
                        variables.Add(keys[j].Key, w.Values[j]);
                    Configs.Add(type, variables);
                }
                Notifiers[i].NotificationManager.SetConfigurationValues(w.Values);
            }
        }

        private void enabled_CheckedChanged(object sender, RoutedEventArgs e)
        {
            CheckBox enabled = (CheckBox)sender;
            bool? isChecked = enabled.IsChecked;
            ((Notifier)enabled.Tag).Enabled = isChecked != null && isChecked.Value;
        }

        private void done_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }
    }
}
